package com.css.dzj.dzjap.modules.lawenforcesuper.service.impl;

import com.css.dzj.dzjap.modules.info.entity.ApExpertEntity;
import com.css.dzj.dzjap.modules.info.service.ExpertService;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ExpregulatConnectExpertEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.QExpregulatConnectExpertEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.mapper.ExpregulatConnectExpertMapper;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.ExpregulatConnectExpertService;
import com.css.dzj.dzjap.modules.lawenforcesuper.view.ExtractExpertView;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 审查专家监管、专家关联serviceimpl
 */
@Service
public class ExpregulatConnectExpertServiceImpl implements ExpregulatConnectExpertService {

    @Autowired
    private ExpregulatConnectExpertMapper connectExpertMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private ExpertService expertService;

    /**
     * 保存
     * @param connectExpertEntity
     */
    @Override
    public void save(ExpregulatConnectExpertEntity connectExpertEntity) {
        connectExpertMapper.save(connectExpertEntity);
    }

    /**
     * 通过监查专家任务Id分页查询专家view
     * @param pageable
     * @param regulatId
     * @return
     */
    @Override
    public QueryResults<ExtractExpertView> queryExpertViewByRegulateId(Pageable pageable, String regulatId) {

        List<ExtractExpertView> extractExpertViewList = new ArrayList<>();
        QExpregulatConnectExpertEntity qConnectExpert = QExpregulatConnectExpertEntity.expregulatConnectExpertEntity;
        QueryResults<ExpregulatConnectExpertEntity> connectExpertEntityQueryResults = jpaQueryFactory.selectFrom(qConnectExpert).where(qConnectExpert.regulatId.eq(regulatId))
                .orderBy(qConnectExpert.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        List<ExpregulatConnectExpertEntity> connectExpertEntityList = connectExpertEntityQueryResults.getResults();
        if(connectExpertEntityList != null && connectExpertEntityList.size() > 0) {
            for (ExpregulatConnectExpertEntity connnectEntity: connectExpertEntityList) {
                if(StringUtils.isEmpty(connnectEntity.getExpertId())) { continue; }
                ApExpertEntity apExpertEntity = expertService.findById(connnectEntity.getExpertId());
                ExtractExpertView extractExpertView = new ExtractExpertView();
                extractExpertView.setExpertId(connnectEntity.getExpertId());
                extractExpertView.setTaskId(connnectEntity.getRegulatId());
                extractExpertView.setCqStatus(connnectEntity.getCqStatus());
                extractExpertView.setExpertName(apExpertEntity.getExpertName());
                extractExpertView.setExpertLevl(apExpertEntity.getExpertLevl());
                extractExpertView.setExpertSeniority(apExpertEntity.getExpertSeniority());
                extractExpertView.setExpertSex(apExpertEntity.getExpertSex());
                extractExpertView.setWorkTime(apExpertEntity.getWorkTime());
                extractExpertViewList.add(extractExpertView);
            }
        }

        QueryResults<ExtractExpertView> viewQueryResults = new QueryResults(extractExpertViewList, (long)pageable.getPageSize(), pageable.getOffset(), connectExpertEntityQueryResults.getTotal());

        return viewQueryResults;
    }

    /**
     * 通过专家抽取任务id查询抽取出的专家list
     * @param pageable
     * @param regulatId
     * @return
     */
    @Override
    public QueryResults<ApExpertEntity> queryExpertByRegulateId(Pageable pageable, String regulatId) {

        List<ApExpertEntity> expertList = new ArrayList<>();
        QExpregulatConnectExpertEntity qConnectExpert = QExpregulatConnectExpertEntity.expregulatConnectExpertEntity;
        // 通过抽取任务id分页查询抽取专家、专家关联表
        QueryResults<ExpregulatConnectExpertEntity> connectExpertEntityQueryResults = jpaQueryFactory.selectFrom(qConnectExpert).where(qConnectExpert.regulatId.eq(regulatId))
                .orderBy(qConnectExpert.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        List<ExpregulatConnectExpertEntity> connectExpertEntityList = connectExpertEntityQueryResults.getResults();
        if(connectExpertEntityList != null && connectExpertEntityList.size() > 0) {
            // 遍历关联表获取抽取出的专家list
            for (ExpregulatConnectExpertEntity connnectEntity: connectExpertEntityList) {
                if(StringUtils.isEmpty(connnectEntity.getExpertId())) { continue; }
                ApExpertEntity apExpertEntity = expertService.findById(connnectEntity.getExpertId());
                expertList.add(apExpertEntity);
            }
        }
        QueryResults<ApExpertEntity> expertQueryResults = new QueryResults(expertList, (long)pageable.getPageSize(), pageable.getOffset(), connectExpertEntityQueryResults.getTotal());

        return expertQueryResults;
    }

    /**
     * 通过任务id分页查询专家
     * @param pageable
     * @param taskId
     * @return
     */
    @Override
    public QueryResults<ApExpertEntity> queryExpertByTaskId(Pageable pageable, String taskId) {
        List<ApExpertEntity> expertList = new ArrayList<>();
        QExpregulatConnectExpertEntity qConnectExpert = QExpregulatConnectExpertEntity.expregulatConnectExpertEntity;
        // 通过抽取任务id分页查询抽取专家、专家关联表
        QueryResults<ExpregulatConnectExpertEntity> connectExpertEntityQueryResults = jpaQueryFactory.selectFrom(qConnectExpert).where(qConnectExpert.taskId.eq(taskId))
                .orderBy(qConnectExpert.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        List<ExpregulatConnectExpertEntity> connectExpertEntityList = connectExpertEntityQueryResults.getResults();
        if(connectExpertEntityList != null && connectExpertEntityList.size() > 0) {
            // 遍历关联表获取抽取出的专家list
            for (ExpregulatConnectExpertEntity connnectEntity: connectExpertEntityList) {
                if(StringUtils.isEmpty(connnectEntity.getExpertId())) { continue; }
                ApExpertEntity apExpertEntity = expertService.findById(connnectEntity.getExpertId());
                expertList.add(apExpertEntity);
            }
        }
        QueryResults<ApExpertEntity> expertQueryResults = new QueryResults(expertList, (long)pageable.getPageSize(), pageable.getOffset(), connectExpertEntityQueryResults.getTotal());

        return expertQueryResults;
    }


}
